package com.itgy.apiinterface.controller;


import com.itgy.apiclientsdk.model.User;
import com.itgy.apiclientsdk.util.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/name")
@Slf4j
public class NameController {

    @GetMapping("/")
    public String getNameByGet(String name, HttpServletRequest request)
    {
        return "GET 你的名字是" + name;
    }

    @PostMapping("/")
    public String getNamePost(@RequestParam String name)
    {
        return "POST 你的名字是" + name;
    }

    @PostMapping("/json")
    public String getUserNamePost(@RequestBody User user, HttpServletRequest httpServletRequest)
    {

//        String accessKey = httpServletRequest.getHeader("accessKey");
//        String nonce = httpServletRequest.getHeader("nonce");
//        String timestamp = httpServletRequest.getHeader("timestamp");
//        String sign = httpServletRequest.getHeader("sign");
//        String body = httpServletRequest.getHeader("body");
//
//        //TODO 1.去数据库中拿
//        if(!"90f9e7f9-d3ca-49f9-b1c0-cc7011eddffe".equals(accessKey))
//        {
//
//            log.debug("accessKey");
//            throw new RuntimeException("accessKey");
//        }
//
//        if(Long.parseLong(nonce)>10000)
//        {
//            log.debug("nonce");
//            throw new RuntimeException("nonce");
//        }
//        //时间和当前时间不能超过五分钟
//        if(System.currentTimeMillis() - Long.parseLong(timestamp)>5*60*1000)
//        {
//            log.debug("超时");
//            throw new RuntimeException("访问超时");
//        }
//        //TODO 2.实际查数据库
//        String sign1 = SignUtil.getSign(body, "8a3eb1d8-593f-444c-9841-8e2ffe1977f1");
//        if(!sign1.equals(sign))
//        {
//            log.debug("sign");
//            throw new RuntimeException("sign");
//        }
      String result =  "POST 你的名字是" + user.getName();
      log.info("result==========>"+result);

        return result;
    }

}
